<?php

namespace App\Http\Controllers\manage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use PDO;

class DictionaryController extends Controller
{
    /**
     * 数据字典
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index() {
        //配置数据库
        $dbserver   = env('DB_HOST', '');
        $dbusername = env('DB_USERNAME', '');             //数据库用户名
        $dbpassword = env('DB_PASSWORD', '');             //密码
        $database   = env('DB_DATABASE', '');     //数据库名
        $prex = env('DB_PREFIX', '');
        //下面是连接数据库
        $pdo = new PDO('mysql:host='.$dbserver.';dbname='.$database.';port=3306',$dbusername,$dbpassword);
        $pdo->exec('set names utf8');
        $sql = "show tables";

        $rows = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
        //取得所有的表名
        foreach ($rows as $row){
            //如果用show tables
            $tables[]['TABLE_NAME'] = $row['Tables_in_web1'];
        }
        //循环取得所有表的备注
        foreach ($tables AS $k=>$v) {
            $sql  = 'SELECT * FROM ';
            $sql .= 'INFORMATION_SCHEMA.TABLES ';
            $sql .= 'WHERE ';
            $sql .= "table_name = '{$v['TABLE_NAME']}'  AND table_schema = '{$database}'";
            $result = $pdo->query($sql)->fetch();
            $tables[$k]['TABLE_COMMENT'] = $result['TABLE_COMMENT'];
            $sql  = 'SELECT * FROM ';
            $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
            $sql .= 'WHERE ';
            $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
            $fields = array();
            $result = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
            foreach ($result as $res){
                $fields[] = $res;
            }
            $tables[$k]['COLUMN'] = $fields;
        }
        //打印在页面中
        return view('manage.system.dictionary',['date'=>$tables,'active1'=>16,'active2'=>3]);
        //$html = '';
        //循环所有表拼接字符串
//        foreach ($tables AS $k=>$v) {
//            //$html .= '<p><h2 style="text-align:center;">'.$v['TABLE_NAME']."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;". $v['TABLE_COMMENT'] . ' </h2>';
//            $html .= '<table class="table table-hover">';
//            $html .= '<tr><td width="20%"><h3>'.$v['TABLE_NAME'].'</h3></td><td colspan="5"><h3>'.$v['TABLE_COMMENT'].'</h3></td></tr>';
//            $html .= '<tbody><tr><th>字段</th><th>数据类型</th><th>默认</th><th>允许非空</th><th>自动递增</th><th width="40%">注释</th></tr>';
//            $html .= '';
//
//            foreach ($v['COLUMN'] AS $f) {
//                $html .= '<tr><td>' . $f['COLUMN_NAME'] . '</td>';
//                $html .= '<td>' . $f['COLUMN_TYPE'] . '</td>';
//                $html .= '<td>' . $f['COLUMN_DEFAULT'] . '</td>';
//                $html .= '<td>' . $f['IS_NULLABLE'] . '</td>';
//                $html .= '<td>' . ($f['EXTRA']=='auto_increment'?'是':'否') . '</td>';
//                $html .= '<td>' . $f['COLUMN_COMMENT'] . '</td>';
//                $html .= '</tr>';
//            }
//            $html .= '</tbody></table></p></br>';
//        }




//        echo '<html>
//             <head>
//             <title>' . $title . '</title>
//             <style>
//             body,td,th {font-size:12px;}
//             table{border-collapse:collapse;border:1px solid #000;background:#efefef;width:65%;}
//             table th{text-align:left; font-weight:bold;height:26px; line-height:26px; font-size:18px; border:1px solid #000;background: #A6A6A6;}
//             table td{height:22px; font-size:14px; border:1px solid #000;background-color:#fff;}
//             .c1{ width: 120px;}
//             .c2{ width: 120px;}
//             .c3{ width: 70px;}
//             .c4{ width: 80px;}
//             .c5{ width: 80px;}
//             .c6{ width: 270px;}
//             </style>
//             </head>
//             <body>';
//        echo '<h1 style="text-align:center;">' . $title . '</h1>';
//        echo $html;
//        echo '</body></html>';
    }
}
